package cn.tedu.logistics.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tedu.logistics.bean.Car;
import cn.tedu.logistics.bean.CarType;
import cn.tedu.logistics.service.CarService;
import cn.tedu.logistics.service.CarTypeService;

@Controller
public class CarController {
	@Autowired
	private CarService service;
	@Autowired
	private CarTypeService typeService;
	//车辆信息显示页面
	@RequestMapping("/car")
	public String listCar(ModelMap map) {
		List<Car> list = service.findCar();
		map.addAttribute("list", list);
		return "car";
	}

	//跳转到“新增车辆信息"
	@RequestMapping("/toaddcar")
	public String toAddCar(ModelMap map) {
		List<CarType> types = typeService.findCarType();
		map.addAttribute("types",types);
		return "addcar";
	}

	//新增车辆信息页面
	@RequestMapping("/addcar")
	public String addCar(Car car) {
		car.setCreateTime(new Date());
		car.setUpdateTime(car.getCreateTime());
		service.addCar(car);
		return "redirect:car";
	}

	//删除车辆信息
	@RequestMapping("/delcar")
	public String delCar(String carId) {
		service.delCar(carId);
		return "redirect:car";
	}
 
	//跳转到修改页面
	@RequestMapping("/toupdcar")
	public String toUpdCar(String carId, ModelMap map) {
		Car car = service.selectById(carId);
		map.addAttribute("car", car);	
		List<CarType> types = typeService.findCarType();
		map.addAttribute("types",types);
		return "updcar";
	}

	//修改车辆信息
	@RequestMapping("/updcar")
	public String updCar(Car car) {
		car.setUpdateTime(new Date());
		service.updateCar(car);
		return "redirect:car";
	}

	@RequestMapping("/checkCarId")
	@ResponseBody
	public int checkCarId(String carId) {
		return service.checkCar(carId);
	}
}
